//
//  DHSearchCategoryItemView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchCategoryItemView.h"

@interface DHSearchCategoryItemView()

@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UILabel *titleLabel;

@end

@implementation DHSearchCategoryItemView

- (instancetype)initWithFrame:(CGRect)frame categoryTitle:(NSString *)title categoryImgURLString:(NSString *)urlString
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.imgView) {
            self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8.5, 14, 26, 26)];
            self.imgView.contentMode = UIViewContentModeCenter;
            self.imgView.layer.masksToBounds = YES;
            self.imgView.layer.cornerRadius = 26/2;
        }
        
        if (!self.titleLabel) {
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.imgView.right + 5, 11.5, frame.size.width - self.imgView.right - 6, 31)];
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            self.titleLabel.textColor = [UIColor colorWithHexString:@"#4A494A"];
        }
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.imgView];
        
        [self.imgView dh_setImageURLString:urlString];
        self.titleLabel.text = title;
        
        [self addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)clickAction:(UIControl *)sender{
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10.5, sender.size.width - 10, 33)];
        bgImageView.tag = 1000;
        bgImageView.image = [UIImage imageNamed:@"button_bg_1px"];
        bgImageView.layer.cornerRadius = 33/2;
        bgImageView.layer.masksToBounds = YES;
        bgImageView.alpha = 0;
        [sender insertSubview:bgImageView atIndex:0];
        [self.titleLabel setTextColor:[UIColor whiteColor]];
        [UIView animateWithDuration:0.2 animations:^{
            bgImageView.alpha = 1.0;
        }];
    }else{
        for (UIView *view in sender.subviews) {
            if (view.tag == 1000) {
                [view removeFromSuperview];
            }
        }
        [self.titleLabel setTextColor:[UIColor colorWithHexString:@"#4A494A"]];
    }
}

@end
